//
//  PainterIndexTuov.m
//  LuGong
//
//  Created by 邱成 on 2017/6/15.
//  Copyright © 2017年 YiShi. All rights reserved.
//

#import "PainterIndexTuov.h"

using namespace std;
using namespace ytx;

@implementation PainterIndexTuov

- (void)drawRect:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    IndexData * data = (IndexData *)indexPosition->getData();
    
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    float tempX = rect.getBeginX();
    float tempY = 0;
    float KX = tempX;
    float zoomX;
    float endY = rect.getEndY();
    float lineWidth = style.quoteLineWidth;
    float lineWidthHalf = lineWidth * 0.5f;
    UpDropStatus::UpDropStatus upDropStatus;
    
    vector<double> & tuovData = data->indexData[0];
    
    int startIndex = indexPosition->getStartDataIndex(), endIndex = indexPosition->getEndDataIndex();
    
    int i = startIndex;
    int len = endIndex;
    
    zoomX = rect.getZoomX();
    float width = zoomX <= 1.0f ? 1.0f : zoomX - lineWidth;
    
    double tuov = 0;
    string color;
    for (; i < len; i++) {
        
        upDropStatus = data->getUpDropStatus(i);
        switch (upDropStatus) {
            case UpDropStatus::Up:
                color = style.defaultRed;
                break;
            case UpDropStatus::Drop:
                color = style.defaultGreen;
                break;
            case UpDropStatus::Balance:
                color = style.avgOfVolumeColor;
                break;
        }
        
        tuov = tuovData[i];
        if (tuov != 0 && !isnan(tuov)) {
            tempY = rect.getPxY(tuov);
            [ctx fillRectLeftBottomWithX:KX+lineWidthHalf withY:endY withWidth:width withHeight:endY-tempY withFillColor:[NSString stringWithUTF8String:color.c_str()]];
            //            ctx.setFillStyle(color).fillRect(KX + lineWidthHalf, beginY, width, tempY - beginY, color);
        }
        KX += zoomX;
    }
}

@end
